﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace weikebuluo.DBUtility
{
    public static class TypeConvert
    {
        /// <summary>
        /// 将整型的数据库字段值转换为System.Int32，如果值为DBNull.Value,则转换为 -1
        /// </summary>
        /// <param name="dbValue">整型的数据库值</param>
        /// <returns></returns>
        public static int ToInt32(object dbValue)
        {
            if (dbValue == null || dbValue == DBNull.Value)
            {
                return int.MinValue;
            }
            return Convert.ToInt32(dbValue);
        }

        #region 将Type类型转换为DbType
        public static System.Data.SqlDbType GetSqlDbType(System.Type sysType)
        {
            System.Data.SqlDbType dbType = System.Data.SqlDbType.NVarChar;
            switch (sysType.Name)
            {
                case "String":
                    dbType = System.Data.SqlDbType.NVarChar;
                    break;
                case "Int32":
                    dbType = System.Data.SqlDbType.Int;
                    break;
                case "DateTime":
                    dbType = System.Data.SqlDbType.DateTime;
                    break;
                case "Double":
                    dbType = System.Data.SqlDbType.Float;
                    break;
                case "Decimal":
                    dbType = System.Data.SqlDbType.Decimal;
                    break;
                case "Byte":
                    dbType = System.Data.SqlDbType.Char;
                    break;
                case "Boolean":
                    dbType = System.Data.SqlDbType.Bit;
                    break;
                case "Guid":
                    dbType = System.Data.SqlDbType.UniqueIdentifier;
                    break;
                default:
                    break;
            }
            return dbType;
        }
        #endregion 将Type类型转换为DbType

        /// <summary>
        /// 类型转换
        /// </summary>
        /// <param name="t"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object TypeRransition(Type type, object value)
        {
            object obj = null;
            if (!Convert.IsDBNull(value))
            {
                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
                {
                    Type t = type.GetGenericArguments()[0];
                    string a = t.Name;
                    obj = TypeRransition(t, value);
                }
                else
                {
                    try
                    {
                        if (type.Name != "Int32" && !string.IsNullOrEmpty(value.ToString()))
                            obj = Convert.ChangeType(value, type);
                        else if (type.Name == "Int32" && !string.IsNullOrEmpty(value.ToString()))
                        {
                            obj = Convert.ChangeType(value, type);
                        }
                    }
                    catch { }
                }
            }
            return obj;
        }
    }
}
